package oj;

import java.util.ArrayList;

public class Day48S {
    public int[] clockwisePrint(int[][] mat, int n, int m) {
        // write code here
        int top=0;
        int bottom=n-1;
        int left=0;
        int right=m-1;
        int[] arr=new int[n*m];
        ArrayList<Integer> list=new ArrayList<>();
        while(top<(n+1)/2 && left<(m+1)/2){
            for(int i=left;i<=right;i++){
                list.add(mat[top][i]);
            }
            for(int i=top+1;i<=bottom;i++){
                list.add(mat[i][right]);
            }
            for(int i=right-1;i>=left && top!=bottom;i--){
                list.add(mat[bottom][i]);
            }
            for(int i=bottom-1;i>=top+1 && left!=right;i--){
                list.add(mat[i][left]);
            }
            top++;
            bottom--;
            left++;
            right--;
        }
        for(int i=0;i<arr.length;i++){
            arr[i]=list.get(i);
        }
        return arr;
    }
}
